package com.ludaxu.google.tools;

import com.ludaxu.google.BaseApplication;
import com.ludaxu.google.R;
import com.ludaxu.google.R.id;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.View;

public class UiUtils {
	/**
	 * 获取字符数组
	 * 
	 * @param tabNames
	 *            字符数组的id
	 * @return
	 */
	public static String[] getStringArray(int tabNames) {

		return getResource().getStringArray(tabNames);

	}

	public static Resources getResource() {
		return BaseApplication.getApplication().getResources();
	}

	public static Context getContext() {
		return BaseApplication.getApplication();
	}

	/**
	 * dip转化成px dp=px/像素密度
	 * 
	 * @return
	 */
	public static int dip2px(int dip) {
		final float density = getResource().getDisplayMetrics().density;
		return (int) (dip * density + 0.5f);
	}

	/**
	 * px转化成dip
	 */
	public static int px2dip(int px) {
		final float density = getResource().getDisplayMetrics().density;
		return (int) (px / density + 0.5f);

	}

	/**
	 * 把Runnable提交到主线程运行
	 * 
	 * @param runnable
	 */
	public static void runOnUiThread(Runnable runnable) {
		// 如果在主线程运行
		if (android.os.Process.myTid() == BaseApplication.getMainTid()) {
			runnable.run();
		} else {// 在子线程
				// 获取handler,将runnable提交到主线程运行
			BaseApplication.getHandler().post(runnable);

		}
	}

	public static View inflate(int id) {
		return View.inflate(getContext(), id, null);
	}

	@SuppressWarnings("deprecation")
	public static Drawable getDrawable(int id) {
		return getResource().getDrawable(id);
	}

	/**
	 * 延迟执行任务
	 * 
	 * @param run
	 * @param time
	 */
	public static void postDelayed(Runnable run, int time) {
		BaseApplication.getHandler().postDelayed(run, time);
	}

	/**
	 * 取消任务
	 * 
	 * @param run
	 */
	public static void Cancel(Runnable run) {
		BaseApplication.getHandler().removeCallbacks(run);
	}

}
